<?php
class GradeModel extends Model {

    /**
     * GradeModel class constructor.
     */
    function GradeModel()
    {
        parent::Model();

    }

    /**
     * Method for inserting a new grade in the database.
     * @param array $gradeData
     * @param int $student_fk
     * @param string $username
     * @return array or boolean
     */
    function addNewGrade($gradeData, $student_fk, $username)
    {
        $this->db->select('students.pk');
        $this->db->from('students');
        $this->db->join('users', 'students.user_fk = users.pk', 'inner');
        $this->db->where('users.username', $username);
        $this->db->where('students.pk', $student_fk);
        $query = $this->db->get();

       //Verify that the student actually belongs to the user the grade is inserted for.
       if($query->num_rows == 0)
                return false;
       else
       {
            $gradeData['student_fk'] = $student_fk;
            $this->db->insert('grades', $gradeData);
       }

        
    }

    /**
     * Method for getting all grades added by a particular user.
     * @param string $username
     * @return array
     */
    function getGradesByUsername($username)
    {
        $this->db->select('students.fname, students.lname, grades.assignmentname,
                           grades.subject, grades.percentscore, grades.lettergrade,
                           grades.pointsawarded, grades.pointspossible, grades.notes');
        $this->db->from('students');
        $this->db->join('users', 'students.user_fk = users.pk', 'inner');
        $this->db->join('grades', 'students.pk = grades.student_fk', 'inner');
        $this->db->where('users.username', $username);
        $query = $this->db->get();
        $query = $query->result_array();

        return $query;
    }
}
?>
